//index.js
//获取应用实例
var util = require('../../utils/util.js');
var nowDate = require('../../utils/date.js');
var formatLocation = util.formatLocation;
var app = getApp()

Page({
  data: {
    motto: 'Hello World123',
    userInfo: {},
    phone: "座机：0208234567",
    mobile: "手机：13527888686",
    sDate: nowDate(2).nowMonth + '月' + nowDate(2).nowDay + '日',
    sTime: '',
    isTime: false,
    userName: '',
    uerPhone: '',
    timer: null,
    hasLocation: false,
    totalPrice:"",
    btnloading:false,
    disabled:true,
    carType: '0',
    carIypeInfo: '小型车(0.6-0.8吨)',
    carIntro: '适用于没有家具电器的微型搬家',
    price: '260',
    addFrom: '',
    addTo: '',
    latFrom: '',
    lngFrom: '',
    latTo: '',
    lngTo: '',
    dis: '0',
    addType: 1,
  },
  toTel: function () {
    wx.makePhoneCall({
      phoneNumber: '02082834567' //仅为示例，并非真实的电话号码
    })
  },
  onShareAppMessage: function () {
    return {
      title: '广州大众搬家',
      path: '/pages/index/index',
      success: function (res) {
        // 分享成功
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },

  carSelect: function () {
    var self = this;
    wx.showActionSheet({
      itemList: ['小型车(0.6-0.8吨)', '中型车(1吨封闭货车)', '中型车(1.5吨敞篷车)', '厢货车(2吨封闭货车)'],
      success: function (res) {
        switch (res.tapIndex) {
          case 0:
            self.setData({
              carIntro: '适用于没有家具电器的微型搬家',
              price: '260',
              carType: '0',
              carIypeInfo: '小型车(0.6-0.8吨)'
            })
            break;
          case 1:
            self.setData({
              carIntro: '适用于少量电器的小型搬家',
              price: '400',
              carType: "1",
              carIypeInfo: '中型车(1吨封闭货车)'
            })
            break;
          case 2:
            self.setData({
              carIntro: '适用于少量电器和家具的中型搬家',
              price: '550',
              carType: "2",
              carIypeInfo: '中型车(1.5吨敞篷车)'
            })
            break;
          case 3:
            self.setData({
              carIntro: '适用于较多电器和家具的家庭及企业搬家',
              price: '1050',
              carType: "3",
              carIypeInfo: '厢货车(2吨封闭货车)'
            })
            break;
        }
        self.calTotal()
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  timeSelect: function () {
    wx.navigateTo({
      url: '/pages/date/time',
    })
  },
  onShow: function () {
    if (app.globalData.fromIndex){
      this.setData({
        addFrom: app.globalData.addFrom,
        addTo: app.globalData.addTo,
        carIypeInfo: app.globalData.carType,
        dis: app.globalData.dis,
        price: app.globalData.carPrice,
        carIntro: app.globalData.carInfo
      });
      app.globalData.fromIndex = null;
    }
    this.data.totalPrice = app.globalData.totalPrice;
    console.log(this.data.totalPrice)
    
  },
  onLoad: function () {

  },
  tapAdd: function (e) {
    var self = this;
    this.data.addType = e.target.dataset.type;
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        if (self.data.addType == 1) {
          self.setData({
            hasLocation: true,
            latFrom: res.latitude,
            lngFrom: res.longitude,
            addFrom: res.name
          })
        } else {
          self.setData({
            hasLocation: true,
            latTo: res.latitude,
            lngTo: res.longitude,
            addTo: res.name
          })
        }
        if (self.data.latFrom != '' && self.data.latTo != '') {
          self.calDis()
        }
      }
    })

  },
  calDis: function () {
    var self = this;
    wx.showLoading({
      title: '计算距离中...',
    })
    wx.request({
      url: util.baiduUrl,
      data: {
        origin: util.transPos(self.data.latFrom, self.data.lngFrom),
        destination: util.transPos(self.data.latTo, self.data.lngTo),
        ak: util.baiduKey
      },
      method: 'GET',
      success: function (data) {
        self.setData({
          dis: ((data.data.result.routes[0].distance) / 1000).toFixed(1)
        });
        wx.hideLoading()
      },
      fail: function (data) {
        wx.hideLoading()
      }
    })
  },
  toTel: function () {
    wx.makePhoneCall({
      phoneNumber: '02082834567' //仅为示例，并非真实的电话号码
    })
  },

  keyInNameInfo: function (e) {
    var self = this;
    if (e.detail.value == "") {
      self.setData({
        userName: ''
      })
    } else {
      self.setData({
        userName: e.detail.value
      })
    }
  },
  keyInPhoneInfo: function (e) {
    var self = this;
    if (e.detail.value == "") {
      self.setData({
        userPhone: ''
      })
    } else {
      self.setData({
        userPhone: e.detail.value
      })
    }
  },
  toCal: function (e) {
    var self = this; 
    if (self.data.addFrom == '' || self.data.addTo == '') {
      wx.showToast({
        title: '请填写地址'
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/cal/cal?from=' + self.data.addFrom + '&to=' + self.data.addTo + '&dis=' + self.data.dis 
    })
  },
  //预约
  submitform: function (e) {
    var self = this;
    if (self.data.addFrom == '' || self.data.addTo == '') {
      wx.showToast({
        title: '请填写地址'
      })
      return;
    }
    if (!self.data.isTime) {
      wx.showToast({
        title: '请选择预约时间'
      })
      return;
    }
    if (self.data.userName == '') {
      wx.showToast({
        title: '请填写姓名',
        image: '../../images/rem.png'
      })
      return;
    }
    var re = /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$/;
    if (!(re.test(self.data.userPhone)) || !self.data.userPhone) {
      wx.showToast({
        title: '手机号码错误',
        image: '../../images/rem.png'
      })
      return;
    };
     if (!self.data.timer) {
      self.data.timer = new Date().getTime();
      setTimeout(function () {
        self.data.timer = null;
      }, 5000)
    } else {
      if (new Date().getTime() - self.data.timer < 5000) {
        wx.showToast({
          title: '操作太频繁',
          image: '../../images/rem.png'
        })
        return;
      }
    }
    wx.showLoading({
      title: '预约中',
    })
    this.data.btnloading=true;
    this.data.disabled=true;
    var orderData = {
      username: self.data.userName,
      phone: self.data.userPhone,
      addfrom: self.data.addFrom,
      addto: self.data.addTo,
      booktime: self.data.sDate + self.data.sTime,
      totalPrice: self.data.totalPrice,
      carType: self.data.carIypeInfo
    }
    wx.setStorageSync("orderData", JSON.stringify(orderData));
    wx.request({
      url: 'https://www.dzbjapp.com/api/subinfo',
      data: {
        username: self.data.userName,
        phone: self.data.userPhone,
        addfrom: self.data.addFrom,
        addto: self.data.addTo,
        booktime: self.data.sDate + self.data.sTime,
        oid: wx.getStorageSync('oid'),
        dis: self.data.dis,
        formid: e.detail.formId,
        totalPrice:self.data.totalPrice,
        carType: self.data.carIypeInfo
      },
      method: 'POST',
      success: function (data) {
       
        if (data.data.status == 100) {
          self.data.disabled = false;
          self.data.btnloading = false;
          wx.navigateTo({
            url: '/pages/book/suc',
          })       
        } else {
          wx.showToast({
            title: data.data.msg,
          })
          self.data.disabled = false;
          self.data.btnloading = false;
        }
        wx.hideLoading()
      }
    })
  }

})

